<template>
<div class="cart">
    <div class="header">
        <div class="title">购物车</div>
        <div class="subtitle">尽情探索，欢迎光临我们的在线商店、感谢您选择我们、让我们一同开启愉悦的购物之旅！</div>
    </div>
    <div class="body">
        <div class="cart-type">
            <div class="cart-type-box">
                <div
                    class="item"
                    :class="{active: state.params.type == 1}"
                    @click="cartType(1)"
                >
                    会员限时套餐
                </div>
                <div
                    class="item"
                    :class="{active: state.params.type == 2}"
                    @click="cartType(2)"
                >
                    叠加永久次卡
                </div>
            </div>
        </div>
        <div v-if="state.loading" class="cart-list">
            <div class="cart-item" v-for="item in 4" :key="item">
                <div class="cover">
                    <a-skeleton-image class="skeleton-image" />
                </div>
                <div class="desc">
                    <a-skeleton active :paragraph="{ rows: 2 }" />
                </div>
                <div class="info">
                    <a-skeleton active :paragraph="{ rows: 2 }" />
                </div>
                <div class="info1">
                    <a-skeleton active :paragraph="{ rows: 1 }" />
                </div>
            </div>
        </div>
        <div v-else class="cart-list">
            <div class="cart-item" v-for="item in state.data" :key="item">
                <div class="cover">
                    <img :src="fullUrl(item.image)" alt="">
                    <div class="title">{{ item.title }}</div>
                </div>
                <div class="desc">
                    <div>{{ item.description }}</div>
                    <span>￥{{ item.price }}</span>
                </div>
                <div class="info">
                    <div class="item">
                        <span>每日问答</span>
                        <span>{{ item.chat_num }} 次</span>
                    </div>
                    <div class="item">
                        <span>每日绘画</span>
                        <span>{{ item.draw_num }} 次</span>
                    </div>
                </div>
                <div class="info1">
                    <div class="item">套餐有效期<span>{{item.days}} 天</span></div>
                    <div class="item">
                        <el-button type="primary" @click="onBuy(item)">立即购买</el-button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script setup lang="ts">
import {inject, onMounted, reactive, ref} from 'vue'
import {useUserInfo} from "/@/stores/userInfo";
import { get_vip, buy_vip } from '/@/api/frontend/chat/user'
import {fullUrl} from "/@/utils/common"
import {ElMessage, ElMessageBox} from "element-plus";
import {useRoute, useRouter} from "vue-router";

const route = useRoute()
const router = useRouter()
const userInfo = useUserInfo()

const state: {
    loading: boolean,
    login_show: boolean,
    data: object,
    params: any,
} = reactive({
    loading: false,
    login_show: inject('login_show', ref(false)),
    data: [],
    params: {
        type: 1,
    }
})

const get_vip_list = () => {
    state.loading = true
    get_vip(state.params).then(res => {
        state.loading = false
        state.data = res.data
    })
}

onMounted(() => {
    get_vip_list()
})

const cartType = (type: number) => {
    state.params.type = type
    get_vip_list()
}

const onBuy = (item: any) => {
    //console.log(item)
    // 判断是否登录
    if (!userInfo.isLogin()) {
        state.login_show = !state.login_show
        return false
    }

    ElMessageBox.confirm('确认购买会员套餐？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success',
        draggable: true,
    }).then(() => {
        buy_vip({id: item.id}).then(res => {
            //ElMessage.success(res.msg)
            ElMessageBox.alert(res.msg, '提示', {
                confirmButtonText: '确定',
                type: 'success',
                callback: () => {
                    // 跳转到users
                    router.push({name: 'users', query: {tab: 'package'}})
                }
            })
        })
    })
}
</script>

<style scoped lang="scss">
:deep(.skeleton-image) {
    width: 100%;
    height: 100%;
    .ant-skeleton-image {
        width: 100%;
        height: 100%;
    }
}

.cart {
    height: 100%;
    background-color: var(--el-bg-color);
    overflow-y: auto;
    .header {
        padding: 16px 25px;
        border-bottom: 1px solid var(--el-border-color);
        margin-left: 10px;
        .title {
            font-size: 18px;
            font-weight: 600;
            color: #555;
        }
        .subtitle {
            font-size: 14px;
            color: #666;
            margin-top: 10px;
        }
    }
    .body {
        padding: 0 112px 60px 112px;
        .cart-type {
            height: 180px;
            display: flex;
            justify-content: center;
            align-items: center;
            .cart-type-box {
                display: flex;
                justify-content: space-between;
                align-items: center;
                width: 400px;
                background-color: #f7f7fa;
                padding: 3px;
                border-radius: 8px;
                .item {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 100%;
                    padding: 6px 0;
                    cursor: pointer;
                    border-radius: 8px;
                    transition: all .3s ease-in-out;
                    &.active {
                        background-color: var(--el-bg-color);
                        animation: shake 1s;
                    }
                }
            }
        }
        .cart-list {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            grid-gap: 10px;
            .cart-item {
                border-radius: 8px;
                overflow: hidden;
                border: 1px solid var(--el-border-color);
                cursor: pointer;
                .cover {
                    position: relative;
                    width: 100%;
                    height: 220px;
                    overflow: hidden;
                    img {
                        position: absolute;
                        top: 0;
                        left: 0;
                        width: 100%;
                        height: 100%;
                        transition: all .3s ease-in-out;
                        &:hover {
                            transform: scale(1.1);
                        }
                    }
                    .title {
                        position: absolute;
                        bottom: 0;
                        left: 0;
                        width: 100%;
                        height: 40px;
                        line-height: 40px;
                        text-align: center;
                        font-size: 16px;
                        font-weight: 600;
                        color: #fff;
                        background-color: rgba(0, 0, 0, .5);
                    }
                }
                .desc {
                    position: relative;
                    height: 160px;
                    padding: 20px;
                    font-size: 18px;
                    border-bottom: 1px solid var(--el-border-color);
                    color: #333639;
                    span {
                        position: absolute;
                        bottom: 20px;
                        right: 20px;
                        font-style: italic;
                        font-weight: 600;
                        color: #ef4444;
                    }
                }
                .info {
                    display: flex;
                    flex-direction: column;
                    padding: 16px;
                    border-bottom: 1px solid var(--el-border-color);
                    .item {
                        display: flex;
                        justify-content: space-between;
                        font-size: 14px;
                        padding: 10px 0;
                        color: #333639;
                    }
                }
                .info1 {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    padding: 16px;
                    .item {
                        font-size: 14px;
                        color: #333639;
                        span {
                            margin-left: 8px;
                            font-size: 18px;
                            font-weight: 500;
                            font-style: italic;
                            color: var(--el-color-primary);
                        }
                    }
                }
            }
        }
    }
}
@media screen and (max-width: 768px) {
    .cart {
        .header {
            padding: 16px 20px;
            .title {
                font-size: 16px;
            }
            .subtitle {
                font-size: 12px;
            }
        }
        .body {
            padding: 0 20px 60px 20px;
            .cart-type {
                height: 100px;
            }
            .cart-list {
                grid-template-columns: repeat(1, 1fr);
                .cart-item {
                    .cover {
                        height: 120px;
                        .title {
                            height: 30px;
                            line-height: 30px;
                            font-size: 14px;
                        }
                    }
                    .desc {
                        height: 80px;
                        font-size: 14px;
                        span {
                            bottom: 10px;
                            right: 10px;
                            font-size: 14px;
                        }
                    }
                    .info {
                        padding: 10px;
                        .item {
                            font-size: 12px;
                            padding: 5px 0;
                        }
                    }
                    .info1 {
                        padding: 10px;
                        .item {
                            font-size: 12px;
                            span {
                                font-size: 14px;
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>
